package org.soft.carbon_neutral.service;

import com.alibaba.fastjson.JSON;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.soft.carbon_neutral.config.MqttConfig;
import org.soft.carbon_neutral.entity.DataItem;
import org.soft.carbon_neutral.entity.EquipmentValue;
import org.soft.carbon_neutral.mapper.DataItemMapper;
import org.soft.carbon_neutral.mapper.EquipmentValueMapper;
import org.soft.carbon_neutral.service.impl.EquipmentValueServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Classname PushCallback
 * @Description 消费监听类
 * @Date 2019/4/11 23:31
 * @Created by Jack
 */
@Component
public class PushCallback implements MqttCallback {
    private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);

    public PushCallback(MqttPushClient mqttPushClient) {
        this.mqttPushClient = mqttPushClient;
    }

    private final MqttPushClient mqttPushClient;

    @Autowired
    private MqttConfig mqttConfig;

    private static MqttClient client;

    @Autowired
    private EquipmentValueMapper service;


    @Override
    public void connectionLost(Throwable throwable) {
        // 连接丢失后，一般在这里面进行重连
        logger.info("连接断开，可以做重连");
        if (client == null || !client.isConnected()) {
            mqttConfig.getMqttPushClient();
        }
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        logger.info("接收消息主题 : " + topic);
        logger.info("接收消息Qos : " + mqttMessage.getQos());
        logger.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
        EquipmentValue dataItem = JSON.parseObject(new String(mqttMessage.getPayload()), EquipmentValue.class);
        service.insert(dataItem);
    }

//    @Override
//    public void messageArrived(String topic, MqttMessage mqttMessage) {
//        //subscribe后得到的消息会执行到这里面
//        logger.info("接收消息主题 : " + topic);
//        logger.info("接收消息Qos : " + mqttMessage.getQos());
//        logger.info("接收消息内容 : " + new String(mqttMessage.getPayload()));
//        //        判断是否从sensorData主题过来的数据
//        if (topic.contains("b"))
//        {
//            logger.info("收到传感器数据 序列化并且保存 ");
//
//            Alarm alarm=new Alarm();
//            SwitchAlarm switchAlarm=switchAlarmRepository.findAll().get(0);
//            Threshold threshold=thresholdRepository.findAll().get(0);
//            if(dataItem.getHumidity()>threshold.getThumidity2()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "red");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }
//                alarm.setMessage("超过湿度阈值2");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getTemperature()>threshold.getTtemperature2()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "red");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过温度阈值2");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getNoise()>threshold.getTnoise2()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "red");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过声音阈值2");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getIllumination()>threshold.getTillumination2()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "red");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过光强度阈值2");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getHumidity()>threshold.getThumidity()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "blue");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过湿度阈值1");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getTemperature()>threshold.getTtemperature()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "blue");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过温度阈值1");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getNoise()>threshold.getTnoise()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "blue");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过声音阈值1");
//                alarmRepository.save(alarm);
//            }else if(dataItem.getIllumination()>threshold.getTillumination()){
//                if(switchAlarm.getMsg().equals("1")){
//                    mqttPushClient.publish(2,false,"xf/a", "blue");
//                    mqttPushClient.publish(2,false,"xf/a", "on");
//                }else {
//                    mqttPushClient.publish(2,false,"xf/a", "green");
//                    mqttPushClient.publish(2,false,"xf/a", "off");
//                }
//                alarm.setMessage("超过光强度阈值2");
//                alarmRepository.save(alarm);
//            }else {
//                mqttPushClient.publish(2,false,"xf/a", "green");
//                mqttPushClient.publish(2,false,"xf/a", "off");
//            }
//
//
//        }
//    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
    }
}
